class Solution:
    def containsNearbyDuplicate(self, nums: list, k: int) -> bool:
        mid_dic = dict()
        for index, num in enumerate(nums):
            if num in mid_dic and index - mid_dic[num] <= k:
                return True
            else:
                mid_dic[num] = index
        return False


a = Solution()
print(a.containsNearbyDuplicate(nums=[1, 2, 3, 1], k=3))
print(a.containsNearbyDuplicate(nums=[1, 0, 1, 1], k=1))
print(a.containsNearbyDuplicate(nums=[1, 2, 3, 1, 2, 3], k=2))
